'use strict';

const {Controller} = require('egg');
const ResponseUtil = require('../utils/ResponseUtil');

class TenantController extends Controller {


    async addTenant(){
        let body = this.ctx.request.body;
        if (!body.tenantName || !body.socialCreditCode || !body.legalPerson || !body.phone || !body.password || !body.email) {
            return ResponseUtil.retArgErr();
        }
        const resp = await this.ctx.service.tenant.addTenant(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async editTenant(){
        let body = this.ctx.request.body;
        if (!body.tenantName || !body.socialCreditCode || !body.legalPerson || !body.phone || !body.email || !body.id) {
            return ResponseUtil.retArgErr();
        }
        const resp = await this.ctx.service.tenant.editTenant(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async getTenantList(){
        let query = this.ctx.query;
        query.delFlag = 0;
        const resp = await this.ctx.service.tenant.getTenantList(query);
        return this.ctx.body = ResponseUtil.success(resp.list, resp.total);
    }

    async getTenantDetail(){
        let tenantId = this.ctx.params.tenantId;
        if (!tenantId) {
            return ResponseUtil.retArgErr();
        }
        const tenant = await this.ctx.service.tenant.selectTenantById(tenantId);

        return this.ctx.body = ResponseUtil.success(tenant);
    }

}

module.exports = TenantController;
